<?php
class CategoryAction extends AdminAction {
    //资源目录管理列表
	public function index(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$cate = M('Category');
		$map['catepid'] = 0;
		$list = $cate->where($map)->select();
		$maps['catepid'] = array('gt',0);
		$lists = $cate->where($maps)->select();
		$this->assign("list",$list);
		$this->assign("lists",$lists);
		$this->display();
	}
	
	public function picture(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$cate = M('Category');
		$map['catepid'] = 1;
		$list = $cate->where($map)->order('orders asc')->select();
		foreach($list as $key => $val){
			$maps['catepid'] = $val['cateid'];
			$lists = $cate->where($maps)->order('orders asc')->select();
			$list[$key]['slist'] = $lists;
		}
		$this->assign("catepid",'1');
		$this->assign("model",'picture');
		$this->assign("list",$list);
		$this->display();
	}
	
	public function video(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$cate = M('Category');
		$map['catepid'] = 2;
		$list = $cate->where($map)->order('orders asc')->select();
		foreach($list as $key => $val){
			$maps['catepid'] = $val['cateid'];
			$lists = $cate->where($maps)->order('orders asc')->select();
			$list[$key]['slist'] = $lists;
		}
		$this->assign("catepid",'2');
		$this->assign("model",'video');
		$this->assign("list",$list);
		$this->display();
	}
	
	public function flash(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$cate = M('Category');
		$map['catepid'] = 3;
		$list = $cate->where($map)->order('orders asc')->select();
		foreach($list as $key => $val){
			$maps['catepid'] = $val['cateid'];
			$lists = $cate->where($maps)->order('orders asc')->select();
			$list[$key]['slist'] = $lists;
		}
		$this->assign("catepid",'3');
		$this->assign("model",'flash');
		$this->assign("list",$list);
		$this->display();
	}
	
	public function game(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$cate = M('Category');
		$map['catepid'] = 4;
		$list = $cate->where($map)->order('orders asc')->select();
		foreach($list as $key => $val){
			$maps['catepid'] = $val['cateid'];
			$lists = $cate->where($maps)->order('orders asc')->select();
			$list[$key]['slist'] = $lists;
		}
		$this->assign("catepid",'4');
		$this->assign("model",'game');
		$this->assign("list",$list);
		$this->display();
	}
	
	public function article(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$cate = M('Category');
		$map['catepid'] = 5;
		$list = $cate->where($map)->order('orders asc')->select();
		foreach($list as $key => $val){
			$maps['catepid'] = $val['cateid'];
			$lists = $cate->where($maps)->order('orders asc')->select();
			$list[$key]['slist'] = $lists;
		}
		$this->assign("catepid",'5');
		$this->assign("model",'article');
		$this->assign("list",$list);
		$this->display();
	}
	
	public function image(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$cate = M('Category');
		$map['catepid'] = 6;
		$list = $cate->where($map)->order('orders asc')->select();
		foreach($list as $key => $val){
			$maps['catepid'] = $val['cateid'];
			$lists = $cate->where($maps)->order('orders asc')->select();
			$list[$key]['slist'] = $lists;
		}
		$this->assign("catepid",'6');
		$this->assign("model",'image');
		$this->assign("list",$list);
		$this->display();
	}
	
	public function book(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$cate = M('Category');
		$map['catepid'] = 7;
		$list = $cate->where($map)->order('orders asc')->select();
		foreach($list as $key => $val){
			$maps['catepid'] = $val['cateid'];
			$lists = $cate->where($maps)->order('orders asc')->select();
			$list[$key]['slist'] = $lists;
		}
		$this->assign("catepid",'7');
		$this->assign("model",'book');
		$this->assign("list",$list);
		$this->display();
	}
	
	public function exhibits(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$cate = M('Category');
		$map['catepid'] = 8;
		$list = $cate->where($map)->order('orders asc')->select();
		foreach($list as $key => $val){
			$maps['catepid'] = $val['cateid'];
			$lists = $cate->where($maps)->order('orders asc')->select();
			$list[$key]['slist'] = $lists;
		}
		$this->assign("catepid",'8');
		$this->assign("model",'exhibits');
		$this->assign("list",$list);
		$this->display();
	}
	
	public function knowledge(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$cate = M('Category');
		$map['catepid'] = 9;
		$list = $cate->where($map)->order('orders asc')->select();
		foreach($list as $key => $val){
			$maps['catepid'] = $val['cateid'];
			$lists = $cate->where($maps)->order('orders asc')->select();
			$list[$key]['slist'] = $lists;
		}
		$this->assign("catepid",'9');
		$this->assign("model",'knowledge');
		$this->assign("list",$list);
		$this->display();
	}
    
	//新增资源目录
	public function addcate()
	{
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$cate = D('Category');
		if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
		{
			$cond = $_POST;
			$cond['_column'] = array('catename','engname','catepid','remark','status','orders');
			foreach ($cond['_column'] as $one) $data[$one] = trim($cond[$one]);
			if($cate->create($data))
			{
				if(($uid = $cate->add()) != false)
				{
					if($_POST['model']){
						$model = 'Category/'.$_POST['model'];
					}
					$this->redirect($model);
				}
			}else
			{
				$this->assign('erorsta','1');
				$this->assign('error',$cate->getError());
			}
		}
		$map['catepid'] = $_GET['catepid'];
		$list = $cate->field('cateid,catename')->where($map)->select();
		$this->assign("list",$list);
		$this->assign('catepid',$_GET['catepid']);
		$this->assign('model',$_GET['model']);
		$this->display();
	}
	
	//编辑资源目录
	public function editcate()
	{
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$cateid = $_GET['cateid'];
		$mapm['cateid'] = $cateid;
		$res = M('Category')->where($mapm)->find();
		$catepid = $_GET['catepid'];
		if($cateid)
		{
			$cate = D('Category');
			if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
			{
				$data = array(
					"cateid"=>$_GET['cateid'],
					"catename"=>$_POST['catename'],
					"engname"=>$_POST['engname'],
					"catepid"=>$_POST['cateppid'],
					"remark"=>$_POST['remark'],
					"status"=>$_POST['status'],
					"orders"=>$_POST['orders'],
				);
				if($cate->create($data)){
					if($_POST['model']){
						$model = 'Category/'.$_POST['model'];
					}
					if($cate->save()){
						$this->redirect($model);
					}else{
						$this->redirect($model);
					}
				}else{
					$this->assign('erorsta','1');
					$this->assign('error',$cate->getError());
				}
				
			}
			if($res['catepid'] == $catepid){
				$mapss['cateid'] = array('neq',$cateid);
			}else{
				$map['cateid'] = array('neq',$res['catepid']);
			}
			$maps['cateid'] = $cateid;
			$info = $cate->where($maps)->find();
			$mapss['catepid'] = $_GET['catepid'];
			$lists = $cate->where($mapss)->select();
			$this->assign("catepid",$catepid);
			$this->assign("info",$info);
			$this->assign("lists",$lists);
			if($_POST['model']){
				$this->assign('model',$_POST['model']);
			}else{
				$this->assign('model',$_GET['model']);
			}
			$this->display();
		}else{
			$this->redirect('Category/index');
		}
	}
	
	//禁用目录
	public function forbidden()
	{
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$cateid = $_POST['cateid'];
		
		if($cateid)
		{
			$map['cateid'] = $cateid;
			$map["status"] = 1;
			$data["status"] = 0;
			$res = M('Category')->where($map)->save($data);
			if($res) echo 1;
			else echo 0;
		}

		else echo 0;
	}
	
	//启用目录
	public function enable()
	{
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$cateid = $_POST['cateid'];
		
		if($cateid)
		{
			$map['cateid'] = $cateid;
			$map["status"] = 0;
			$data["status"] = 1;
			$res = M('Category')->where($map)->save($data);
			if($res) echo 1;
			else echo 0;
		}

		else echo 0;
	}
	
	//删除目录
	public function dele()
	{
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$cateid = $_POST['cateid'];
		
		if($cateid)
		{
			$map['cateid'] = $cateid;
			$resl = M('Category')->where($map)->find();
			if($resl['catepid'] == 0){
				echo 0;	
			}else{
				$res = M('Category')->where($map)->delete();
				if($res) echo 1;
				else echo 0;
			}	
		}
		
		else echo 0;
	}
	
	//显示资源库下的分类
	
	public function showsid(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$pid = $_POST['pid'];
		if($pid){
			$map['catepid'] = $pid;
			$resl = M('Category')->where($map)->select();
			if($resl){
				$res = "<option value='0' selected >新建分类</option>";
				foreach ($resl as $key => $value){
					$res .= "<option value='". $value['cateid'] ."' selected>" . $value['catename'] . "</option>";
				}
				echo $res;
			}else{
				$res = "<option value='0' selected >新建分类</option>";
				echo $res;
			}
		}else{
			echo 0;
		}
	}
	
}